package it.avantsoft.ls.peer.message;

import java.io.DataOutputStream;
import java.io.IOException;

public class WelcomeMessage extends PeerMessage {
    private int listeningPort;

    public WelcomeMessage(DataOutputStream out, int listeningPort) {
        super(out);
        this.listeningPort = listeningPort;
    }

    /**
     * Sends WelcomeCommand with listening port;
     *
     * @return true if whe message has been sent
     */
    public boolean send() {
        boolean sent = false;
        try {
            writeByte(WELCOME);
            writeInt(listeningPort);
            flush();
            sent = true;
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return sent;
    }

    public String toString() {
        return "WelcomeMessage{" +
                "listeningPort=" + listeningPort +
                '}';
    }
}
